
package com.iplant.basic.controller.mss;

import com.iplant.basic.service.MSSService;
import com.iplant.basic.service.po.mss.MSSMaterial;
import com.iplant.basic.service.po.mss.MSSMaterialGroup;
import com.iplant.basic.service.po.mss.MSSMaterialType;
import com.iplant.base.controller.BaseController;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.BMSService;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Deprecated
@RestController
@RequestMapping("/api/Material")
public class MaterialController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(MaterialController.class);

	@Autowired
	MSSService wMSSService;

	@Autowired
	BMSService wBMSService;

	@GetMapping("/All")
	Object All(HttpServletRequest request) {
		Map<String, Object> wResult = new HashMap<String, Object>();
		try {

			BMSEmployee wLoginUser = GetSession(request);

			String wMaterialNoLike = StringUtils.parseString(request.getParameter("MaterialNo"));
			String wMaterialNameLike = StringUtils.parseString(request.getParameter("MaterialName"));

			int wMaterialTypeID = StringUtils.parseInt(request.getParameter("MaterialTypeID"),-1);
			int wMaterialGroupID = StringUtils.parseInt(request.getParameter("MaterialGroupID"),-1);
			int wTypeID = StringUtils.parseInt(request.getParameter("TypeID"),-1);
			int wActive = StringUtils.parseInt(request.getParameter("Active"),-1);
			int wRecoil = StringUtils.parseInt(request.getParameter("Recoil"),-1);
			int wOutsourced = StringUtils.parseInt(request.getParameter("Outsourced"));

			String wMaterialFullGroup = StringUtils.parseString(request.getParameter("MaterialFullGroup"));
			String wMaterialMode = StringUtils.parseString(request.getParameter("MaterialMode"));
			String wDrawingNoLike = StringUtils.parseString(request.getParameter("DrawingNoLike")); 
			Pagination wPagination = GetPagination(request);

			ServiceResult<List<MSSMaterial>> wServiceResult = wMSSService.MSS_QueryMaterialList(wLoginUser,
					wMaterialNoLike, wMaterialNameLike, wMaterialTypeID, wMaterialGroupID, wTypeID,
					  wMaterialFullGroup, wMaterialMode, wDrawingNoLike, wRecoil, wOutsourced, wActive,
					wPagination);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(),
						wPagination.getTotalPage());
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}

		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@GetMapping("/Info")
	Object Info(HttpServletRequest request) {
		Map<String, Object> wResult = new HashMap<String, Object>();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			int wMaterialID = StringUtils.parseInt(request.getParameter("ID"));

			String wMaterialNo = StringUtils.parseString(request.getParameter("Code"));

			ServiceResult<MSSMaterial> wServiceResult = wMSSService.MSS_QueryMaterial(wBMSEmployee, wMaterialID,
					wMaterialNo);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.getResult());
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}

		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/Active")
	Object Active(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Map<String, Object> wResult = new HashMap<String, Object>();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			List<MSSMaterial> wMSSMaterialList = CloneTool.CloneArray(wParam.get("data"), MSSMaterial.class);
			int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

			if (wMSSMaterialList == null || wMSSMaterialList.size() < 1) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_IN);
				return wResult;
			}

			ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
			for (MSSMaterial wMSSMaterial : wMSSMaterialList) {
				if (wMSSMaterial.ID <= 0)
					continue;

				wServiceResult = wMSSService.MSS_ActiveMaterial(wBMSEmployee, wMSSMaterial, wActive);
				if (StringUtils.isNotEmpty(wServiceResult.getFaultCode()))
					break;
			}
			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/Update")
	Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Map<String, Object> wResult = new HashMap<String, Object>();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			MSSMaterial wMSSMaterial = CloneTool.Clone(wParam.get("data"), MSSMaterial.class);

			ServiceResult<Integer> wServerRst = wMSSService.MSS_UpdateMaterial(wBMSEmployee, wMSSMaterial);

			if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wMSSMaterial);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/UpdateList")
	Object UpdateList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Map<String, Object> wResult = new HashMap<String, Object>();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			String wUserName = wBMSEmployee.getName();

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}
			List<MSSMaterial> wMSSMaterialList = CloneTool.CloneArray(wParam.get("data"), MSSMaterial.class);

			for (MSSMaterial mssMaterial : wMSSMaterialList) {
				mssMaterial.CreatorID = wBMSEmployee.getID();
				mssMaterial.CreatorName = wUserName;
				mssMaterial.EditorID = wBMSEmployee.getID();
				mssMaterial.EditorName = wUserName;
			}

			ServiceResult<Integer> wServerRst = wMSSService.MSS_UpdateMaterialList(wBMSEmployee, wMSSMaterialList);

			if (StringUtils.isEmpty(wServerRst.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServerRst.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/Delete")
	Object Delete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			MSSMaterial wMSSMaterial = CloneTool.Clone(wParam.get("data"), MSSMaterial.class);

			ServiceResult<Integer> wServiceResult = wMSSService.MSS_DeleteMaterial(wBMSEmployee, wMSSMaterial);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@GetMapping("/GroupAll")
	Object GroupAll(HttpServletRequest request) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);
			int wParentGroupID = StringUtils.parseInt(request.getParameter("ParentGroupID"));
			String wGroupFullName = StringUtils.parseString(request.getParameter("GroupFullName"));
			String wName = StringUtils.parseString(request.getParameter("Name"));
			int wActive = StringUtils.parseInt(request.getParameter("Active"));
			Pagination wPagination = GetPagination(request);
			ServiceResult<List<MSSMaterialGroup>> wServiceResult = wMSSService.MSS_QueryMaterialGroupList(wLoginUser,
					wName, wParentGroupID, wGroupFullName, wActive, wPagination);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@GetMapping("/GroupInfo")
	Object GroupInfo(HttpServletRequest request) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);

			int wID = StringUtils.parseInt(request.getParameter("ID"));
			String wCode = StringUtils.parseString(request.getParameter("Code"));

			ServiceResult<MSSMaterialGroup> wServiceResult = wMSSService.MSS_QueryMaterialGroup(wLoginUser, wID, wCode);
			MSSMaterialGroup wServerRst = wServiceResult.Result;

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wServerRst);
			}

		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/GroupUpdate")
	Object GroupUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);
			int wUserID = wBMSEmployee.ID;
			String wUserName = wBMSEmployee.Name;

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			MSSMaterialGroup wMSSMaterialGroup = CloneTool.Clone(wParam.get("data"), MSSMaterialGroup.class);
			wMSSMaterialGroup.EditorName = wUserName;
			wMSSMaterialGroup.EditorID = wUserID;
			wMSSMaterialGroup.EditTime = Calendar.getInstance();
			if (wMSSMaterialGroup.ID <= 0) {

				wMSSMaterialGroup.CreatorName = wUserName;
				wMSSMaterialGroup.CreatorID = wUserID;
				wMSSMaterialGroup.CreateTime = Calendar.getInstance();

			}
			ServiceResult<Integer> wServiceResult = wMSSService.MSS_UpdateMaterialGroup(wBMSEmployee,
					wMSSMaterialGroup);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wMSSMaterialGroup);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/GroupDelete")
	Object GroupDelete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			MSSMaterialGroup wMSSMaterialGroup = CloneTool.Clone(wParam.get("data"), MSSMaterialGroup.class);

			ServiceResult<Integer> wServiceResult = wMSSService.MSS_DeleteMaterialGroup(wBMSEmployee,
					wMSSMaterialGroup);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/GroupActive")
	Object GroupActive(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

			List<MSSMaterialGroup> wMSSMaterialGroupList = CloneTool.CloneArray(wParam.get("data"),
					MSSMaterialGroup.class);
			ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
			for (MSSMaterialGroup wMSSMaterialGroup : wMSSMaterialGroupList) {

				wServiceResult = wMSSService.MSS_ActiveMaterialGroup(wBMSEmployee, wMSSMaterialGroup, wActive);

				if (!StringUtils.isEmpty(wServiceResult.getFaultCode()))
					break;
			}

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@GetMapping("/TypeAll")
	Object TypeAll(HttpServletRequest request) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);
			String wName = StringUtils.parseString(request.getParameter("Name"));
			int wActive = StringUtils.parseInt(request.getParameter("Active"));
			Pagination wPagination = GetPagination(request);
			ServiceResult<List<MSSMaterialType>> wServiceResult = wMSSService.MSS_QueryMaterialTypeList(wLoginUser,
					wName, wActive, wPagination);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@GetMapping("/TypeInfo")
	Object TypeInfo(HttpServletRequest request) {
		Object wResult = new Object();
		try {

			BMSEmployee wLoginUser = GetSession(request);

			int wID = StringUtils.parseInt(request.getParameter("ID"));
			String wCode = StringUtils.parseString(request.getParameter("Code"));

			ServiceResult<MSSMaterialType> wServiceResult = wMSSService.MSS_QueryMaterialType(wLoginUser, wID, wCode);
			MSSMaterialType wServerRst = wServiceResult.Result;

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wServerRst);
			}

		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/TypeUpdate")
	Object TypeUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);
			int wUserID = wBMSEmployee.ID;
			String wUserName = wBMSEmployee.Name;

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			MSSMaterialType wMSSMaterialType = CloneTool.Clone(wParam.get("data"), MSSMaterialType.class);
			wMSSMaterialType.EditorName = wUserName;
			wMSSMaterialType.EditorID = wUserID;
			wMSSMaterialType.EditTime = Calendar.getInstance();
			if (wMSSMaterialType.ID <= 0) {

				wMSSMaterialType.CreatorName = wUserName;
				wMSSMaterialType.CreatorID = wUserID;
				wMSSMaterialType.CreateTime = Calendar.getInstance();

			}
			ServiceResult<Integer> wServiceResult = wMSSService.MSS_UpdateMaterialType(wBMSEmployee, wMSSMaterialType);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wMSSMaterialType);
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/TypeDelete")
	Object TypeDelete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			MSSMaterialType wMSSMaterialType = CloneTool.Clone(wParam.get("data"), MSSMaterialType.class);

			ServiceResult<Integer> wServiceResult = wMSSService.MSS_DeleteMaterialType(wBMSEmployee, wMSSMaterialType);

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}

	@PostMapping("/TypeActive")
	Object TypeActive(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
		Object wResult = new Object();
		try {

			BMSEmployee wBMSEmployee = GetSession(request);

			if (!wParam.containsKey("data")) {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
				return wResult;
			}

			int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

			List<MSSMaterialType> wMSSMaterialTypeList = CloneTool.CloneArray(wParam.get("data"),
					MSSMaterialType.class);
			ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
			for (MSSMaterialType wMSSMaterialType : wMSSMaterialTypeList) {

				wServiceResult = wMSSService.MSS_ActiveMaterialType(wBMSEmployee, wMSSMaterialType, wActive);

				if (!StringUtils.isEmpty(wServiceResult.getFaultCode()))
					break;
			}

			if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
				wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
			} else {
				wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
		}
		return wResult;
	}


}
